<?php
namespace Home\Controller;
use Think\Controller;
class XiangqingController extends Controller {
    public $Book;
    public $Scang;
    public $Jeshu;
    public function __construct() {
        parent::__construct();
        $this->Book = M('addbook');
        $this->Scang = M('shoucang');
        $this->Jieshu = M('jieshu');
    } 
    public function sy(){
        $id = $_GET['id'];
        $_SESSION['bookid']=$id;
		    $this->xgbook();
		
        $data['id'] = $id;
        $a = $this->Book->where($data)->select();
		    $b = $this->Book->find(I('get.id'));	
        $this->allhistory($b);
        $this->assign('Book',$a);		
        $comment=M('comment');
        $ve=$comment->where("bookid=$id")->order("id desc")->limit(1)->select();
        
        
        // $userid = $_SESSION['id'];
        // $value=$this->Scang->where("userid=$userid AND bookid=$id")->select();
        // if($value){
        //     $this->error("您已收藏过此书！");
        // }

        $this->assign('cc',$ve);
		
        $this->display(); 
    } 
    public function comment(){
        $comment=M('comment');
        $id=$_SESSION['bookid'];
        $value=$comment->where("bookid=$id")->order("id desc")->select();
        $this->assign('tuijianBook',$value);
        // $this->redirect('Xiangqing/sy');
        $this->display();
    }
    public function bookcomment(){
        $id=$_SESSION['bookid'];
        $content=$_POST['comment'];
        $time=time();
        $date=date("Y-m-d H:i:d",$time);
        if($_SESSION['yonghuname']){
            $username=$_SESSION['yonghuname'];
        }else{
            $this->error("您还未登录，不能评论！");
        }
        
        $da=array('username'=>$username,'content'=>$content,'addtime'=>$date,'bookid'=>$id);
        $comment=M('comment');
        $value=$comment->add($da);
        if($value){
            $this->success("发表成功");
        }else{
            $this->error("发表失败");
        }
    }
   
    public function cang(){
          $shoucang=M('shoucang');
          $bookid = $_SESSION['bookid'];
          $userid = $_SESSION['id'];
          $time=time();
          $date=date("Y-m-d H:i:d",$time);
          $value=$shoucang->where("userid=$userid AND bookid=$bookid")->select();
          if(!$userid) {
            $this->error('注册登陆后方可收藏') ;           
          } else {
            if($value){
              echo '<a href="'.U('Xiangqing/deleshoucang').'"><span style="color:#F58611;">您已收藏过</span></a>
                   ';
              
            }else{
              $sc=array('userid'=>$userid,'bookid'=>$bookid,'sctime'=>$date);
              $info = $this->Scang->add($sc);
              echo '<a href="'.U('Xiangqing/deleshoucang').'"><span style="color:#F58611;">取消收藏</span></a> 
                  ';
            }
            
          } 
    }
    public function dsc(){
      if($_POST['id']){
            $bookid = $_POST['id'];
        } else {
            $bookid = $_SESSION['bookid'];
        }      
          $userid = $_SESSION['id'];
          $data['bookid'] = $bookid;  
          $data['userid'] = $userid;
          $sc = $this->Scang->field('id')->where($data)->select();
          $a=$this->Scang->where($data)->delete(); 

          
        //   $iid=$this->Scang->where("userid=$userid")->select();
        //   foreach ($iid as $k => $v) { 
        //      $ids[] = $v['bookid'];
        //  }
        //  $map['id'] = array('in',$ids);
        // $scbook = $this->Book->field('id,title,suotu,writer,note')->where($map)->select();
        // $this->assign('scbook',$scbook);

    }
    public function deleshoucang() {
        if($_GET['id']){
            $bookid = $_GET['id'];
        } else {
            $bookid = $_SESSION['bookid'];
        }      
          $userid = $_SESSION['id'];
          $data['bookid'] = $bookid;  
          $data['userid'] = $userid;
          $sc = $this->Scang->field('id')->where($data)->select();
          $a=$this->Scang->where($data)->delete(); 
          echo "<script>history.back(-1)</script>"; 
    }
	public function xgbook() {
		 $bookid = $_SESSION['bookid'];
         $data['id'] = $bookid;
		 $sid_id = $this->Book->field('sid')->where($data)->select();		
		  $ids = array();
         foreach ($sid_id as $k => $v) {   //把二维数组转化为一维数组
             $ids[] = $v['sid'];
         }
          $map['sid'] = array('in',$ids);
		  $map['id'] = array('not in',$bookid);	  
          $xgBook = $this->Book->field('id,title,suotu,writer')->where($map)->select(); 
 
          if(count($xgBook) >= 6){
               shuffle($xgBook);
               $xgBook1= array_slice($xgBook, 0,6);
               $this->assign('xgBook',$xgBook1);
          } else {
              $this->assign('xgBook',$xgBook);
          }
	}
	
	public function allhistory($b){		
		$g = $b; 
		$history = session('history');		
		if(empty($history)) {
			$history = array();
		}
				
		if(isset($history[$g['id']])) {
			unset($history[$g['id']]);
		}
		
		$row = array();
		
		$row['suotu'] = $g['suotu'];
		$row['title'] = $g['title'];
		$row['writer'] = $g['writer'];
		$row['note'] = $g['note'];
		$history[$g['id']] = $row;
		
		//浏览历史不超过10个
		if(count($history) > 10) {
			$key = key($history);
			unset($history[$key]);
		}		
		session('history',$history);			
	}
	
	public function history(){
		$this->assign('history',array_reverse(session('history'),true));		
		$this->display();
	}

  public function jieyue() {
          
          $bookid = $_SESSION['bookid'];
          $userid = $_SESSION['id'];
          $time=time();
          $date=date("Y-m-d H:i:d",$time);
         
         if($_POST['username']&&$_POST['tele']&&$_POST['qushutime']) {
               $username = $_POST['username'];
             $tele = $_POST['tele'];
             $qushutiem = $_POST['qushutime'];
          $value=$this->Jieshu->where("userid=$userid AND bookid=$bookid")->select();
             $data['userid'] = $userid;
             $count=$this->Jieshu->where($data)->count();
           if($count>=2) {
              $this->error('每人一次只能借两本书，若要借此书请重新调整借书单');
           }
          if($value) {
            $this->error('您已经借阅过此书');             
            }else{   
              $jshu=array('userid'=>$userid,'bookid'=>$bookid,'jieshutime'=>$date,
                 'qushutime'=>$qushutiem,'telephone'=>$tele,'username'=>$username 
                );
              $info = $this->Jieshu->add($jshu);
            } 
            if($info){
                  $this->success('此书加入借书单成功');
            }
          } else {
             $this->error('请您填写完整的预约信息');
          }
         }

         
}